package com.hmdp.utils;

import com.hmdp.utils.properties.ThreadPoolConfigProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @Author xph
 * @Date 2022/7/12 15:01
 * @Description: 线程池配置
 */
@EnableConfigurationProperties(ThreadPoolConfigProperties.class)
@Configuration
public class ThreadPoolConfig {

    @Autowired
    private ThreadPoolConfigProperties poolConfigProperties;

    @Bean
    public ThreadPoolExecutor voucherExecutor() {
        // 抛弃策略为直接抛弃新来的任务，抛出异常
        return new ThreadPoolExecutor(
                poolConfigProperties.getCorePoolSize(),
                poolConfigProperties.getMaxPoolSize(),
                poolConfigProperties.getKeepAliveTime(),
                TimeUnit.SECONDS,
                new LinkedBlockingDeque<>(poolConfigProperties.getQueueCapacity()));
    }
}
